package com.example.teamtribe.main.club.clubmembers;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.database.User;
import com.example.teamtribe.main.club.manage.feedback.apply.AppListActivity;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
public class MembersActivity extends BaseActivity implements MembersContract.View,View.OnClickListener{
    private MembersContract.Presenter presenter;
    private MembersAdapter membersAdapter;
        private List<User> presidentList = new ArrayList<>();
        private List<User> adminList = new ArrayList<>();
        private List<User> memberList = new ArrayList<>();
        private Team team;
        private RefreshLayout refreshLayout;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_club_members);
            //ImageView ivMsg = findViewById(R.id.iv_more);
            //ivMsg.setImageDrawable(getDrawable(R.drawable.ic_msg));
            //ivMsg.setVisibility(View.VISIBLE);
            //ivMsg.setOnClickListener(this);
            ((TextView) findViewById(R.id.tv_title)).setText("社团人员");
            team = (Team) getIntent().getParcelableExtra("team");
            presenter = new MembersPresenter(this);
            RecyclerView members = (RecyclerView) findViewById(R.id.rv_members);
            members.setLayoutManager(new LinearLayoutManager(this));
            membersAdapter = new MembersAdapter();
            presenter.getMembers(team.getRealId(), 1, 10);
            members.setAdapter(membersAdapter);

            refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
            refreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    //refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                    presenter.getMembers(team.getRealId(), 1, 10);
                }
            });
            refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(RefreshLayout refreshlayout) {
                    refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                    //presenter.getMore(team.getRealId(), memberList.size(), 10);
                }
            });
        }

        @Override
        public void addMember(final User user, final int type) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    membersAdapter.addMember(user, type);
                    //Log.d("zsm", "================================ ");
                    membersAdapter.notifyDataSetChanged();
                }
            });
        }
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_more:
                    Intent intent = new Intent(MembersActivity.this, AppListActivity.class);
                    intent.putExtra("teamId", team.getRealId());
                    startActivity(intent);
                    break;

                default:
                    break;
            }
        }
    @Override
    public void finishRefresh() {
        refreshLayout.finishRefresh(0);//传入false表示刷新失败
    }

    @Override
    public void clearMembers() {
        membersAdapter.clear();
    }

    private void initUsers() {
            for (int i = 0; i < 2; i++) {
                User user = new User();
                user.setId(2);
                user.setNickname("two");
                user.setAcademy("ll");
                presidentList.add(user);
            }
            for (int i = 0; i < 2; i++) {
                User user = new User();
                user.setId(3);
                user.setNickname("two");
                user.setAcademy("ll");
                adminList.add(user);
            }
            for (int i = 0; i < 20; i++) {
                User user = new User();
                user.setId(4);
                user.setNickname("3");
                user.setAcademy("ll");
                memberList.add(user);
            }
        }
    }
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onFound(SearchUser searchUser) {
//        membersAdapter.addMember(searchUser.getUser(),1);
//        membersAdapter.notifyDataSetChanged();
//    }

//    @Override
//    public void setnum(int num, int type) {
//        membersAdapter.setNum(num,type);
//    }

//    @Override
//    public void addMembers(List<User> users) {
//
//    }
//
//    @Override
//    public void complete() {
//
//    }
//
//    @Override
//    public void clearMembers() {
//
//    }

